{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.requests(让HTTPS服务人类)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* r 是指 response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"https://cn.bing.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* r.content\n",
    "> 1. 可以获取请求数据内容（HTML，Json)\n",
    "> 2. b是指 bytes：二进制文件\n",
    "* r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(\"http://httpbin.org/get\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.24.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62307181-0affc40c32a1571c50ed223e'},\n",
       " 'origin': '120.85.41.231',\n",
       " 'url': 'http://httpbin.org/get'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json() #将json转换成字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.24.0\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-62307181-0affc40c32a1571c50ed223e\"\\n  }, \\n  \"origin\": \"120.85.41.231\", \\n  \"url\": \"http://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\\n  \"args\": {}, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.24.0\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-62307181-0affc40c32a1571c50ed223e\"\\n  }, \\n  \"origin\": \"120.85.41.231\", \\n  \"url\": \"http://httpbin.org/get\"\\n}\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.text"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 3. payload\n",
    "使用payload来实现请求数据传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    'name':'zhichao',\n",
    "    'JOB':'IT'\n",
    "}\n",
    "\n",
    "\n",
    "r = requests.get(\"http://httpbin.org/get\",params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'args': {'JOB': 'IT', 'name': 'zhichao'},\n",
       " 'headers': {'Accept': '*/*',\n",
       "  'Accept-Encoding': 'gzip, deflate',\n",
       "  'Host': 'httpbin.org',\n",
       "  'User-Agent': 'python-requests/2.24.0',\n",
       "  'X-Amzn-Trace-Id': 'Root=1-62307183-0262fee65f4b6c2a470d1d0a'},\n",
       " 'origin': '120.85.41.231',\n",
       " 'url': 'http://httpbin.org/get?name=zhichao&JOB=IT'}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. face++ detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 准备base url (对应该功能的url链接)\n",
    "face_detect_url =\"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    " \n",
    "# 2. 准备参数数据\n",
    "payload = {\n",
    "    'api_key':'fM1G06dF-zEj3SO2kHxvHjF8ZoMHivBl',\n",
    "    'api_secret':'4lq5-_IsVbNsykzVWdLKwn2BeQZDOYFA',\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7',\n",
    "    'return-landmark':1,\n",
    "    'return_attributes':'gender,age,emotion,beauty'\n",
    "}\n",
    "\n",
    "#   3. 检查调用的请求方法：POST\n",
    "#   4. 请求人脸数据\n",
    "r = requests.post(url = face_detect_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647345167,4376327e-a959-4907-b609-a99238e920ed',\n",
       " 'time_used': 1154,\n",
       " 'faces': [{'face_token': '284024e659cd0e25465a95a8e7c3e74d',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'emotion': {'anger': 0.007,\n",
       "     'disgust': 1.213,\n",
       "     'fear': 0.112,\n",
       "     'happiness': 97.97,\n",
       "     'neutral': 0.007,\n",
       "     'sadness': 0.681,\n",
       "     'surprise': 0.009},\n",
       "    'beauty': {'male_score': 56.694, 'female_score': 56.631}}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "API_key = 'fM1G06dF-zEj3SO2kHxvHjF8ZoMHivBl'\n",
    "API_secret = '4lq5-_IsVbNsykzVWdLKwn2BeQZDOYFA'\n",
    "payload = {\n",
    "   'api_key':'fM1G06dF-zEj3SO2kHxvHjF8ZoMHivBl',\n",
    "    'api_secret':'4lq5-_IsVbNsykzVWdLKwn2BeQZDOYFA',\n",
    "    'face_tokens':'284024e659cd0e25465a95a8e7c3e74d',\n",
    "     'return-landmark':1,\n",
    "    'return_attributes':'gender,age,smiling,eyestatus,emotion,beauty',\n",
    "    'beauty_score_min':0\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647345167,4376327e-a959-4907-b609-a99238e920ed',\n",
       " 'time_used': 1154,\n",
       " 'faces': [{'face_token': '284024e659cd0e25465a95a8e7c3e74d',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'emotion': {'anger': 0.007,\n",
       "     'disgust': 1.213,\n",
       "     'fear': 0.112,\n",
       "     'happiness': 97.97,\n",
       "     'neutral': 0.007,\n",
       "     'sadness': 0.681,\n",
       "     'surprise': 0.009},\n",
       "    'beauty': {'male_score': 56.694, 'female_score': 56.631}}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
